// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.vehicles;

import org.joml.Vector4f;
import zombie.iso.Vector2;

public final class QuadranglesIntersection {
    private static final float EPS = 0.001F;

    public static boolean IsQuadranglesAreIntersected(Vector2[] vector21, Vector2[] vector20) {
        if (vector21 != null && vector20 != null && vector21.length == 4 && vector20.length == 4) {
            if (lineIntersection(vector21[0], vector21[1], vector20[0], vector20[1])) {
                return true;
            } else if (lineIntersection(vector21[0], vector21[1], vector20[1], vector20[2])) {
                return true;
            } else if (lineIntersection(vector21[0], vector21[1], vector20[2], vector20[3])) {
                return true;
            } else if (lineIntersection(vector21[0], vector21[1], vector20[3], vector20[0])) {
                return true;
            } else if (lineIntersection(vector21[1], vector21[2], vector20[0], vector20[1])) {
                return true;
            } else if (lineIntersection(vector21[1], vector21[2], vector20[1], vector20[2])) {
                return true;
            } else if (lineIntersection(vector21[1], vector21[2], vector20[2], vector20[3])) {
                return true;
            } else if (lineIntersection(vector21[1], vector21[2], vector20[3], vector20[0])) {
                return true;
            } else if (lineIntersection(vector21[2], vector21[3], vector20[0], vector20[1])) {
                return true;
            } else if (lineIntersection(vector21[2], vector21[3], vector20[1], vector20[2])) {
                return true;
            } else if (lineIntersection(vector21[2], vector21[3], vector20[2], vector20[3])) {
                return true;
            } else if (lineIntersection(vector21[2], vector21[3], vector20[3], vector20[0])) {
                return true;
            } else if (lineIntersection(vector21[3], vector21[0], vector20[0], vector20[1])) {
                return true;
            } else if (lineIntersection(vector21[3], vector21[0], vector20[1], vector20[2])) {
                return true;
            } else if (lineIntersection(vector21[3], vector21[0], vector20[2], vector20[3])) {
                return true;
            } else if (lineIntersection(vector21[3], vector21[0], vector20[3], vector20[0])) {
                return true;
            } else {
                return IsPointInTriangle(vector21[0], vector20[0], vector20[1], vector20[2])
                        || IsPointInTriangle(vector21[0], vector20[0], vector20[2], vector20[3])
                    ? true
                    : IsPointInTriangle(vector20[0], vector21[0], vector21[1], vector21[2])
                        || IsPointInTriangle(vector20[0], vector21[0], vector21[2], vector21[3]);
            }
        } else {
            System.out.println("ERROR: IsQuadranglesAreIntersected");
            return false;
        }
    }

    public static boolean IsPointInTriangle(Vector2 vector20, Vector2[] vector21) {
        return IsPointInTriangle(vector20, vector21[0], vector21[1], vector21[2]) || IsPointInTriangle(vector20, vector21[0], vector21[2], vector21[3]);
    }

    public static float det(float a, float b, float c, float d) {
        return a * d - b * c;
    }

    private static boolean between(float float0, float float1, double _double) {
        return (double)Math.min(float0, float1) <= _double + 0.001F && _double <= (double)(Math.max(float0, float1) + 0.001F);
    }

    private static boolean intersect_1(float float2, float float3, float float6, float float7) {
        float float0;
        float float1;
        if (float2 > float3) {
            float1 = float2;
            float0 = float3;
        } else {
            float0 = float2;
            float1 = float3;
        }

        float float4;
        float float5;
        if (float6 > float7) {
            float5 = float6;
            float4 = float7;
        } else {
            float4 = float6;
            float5 = float7;
        }

        return Math.max(float0, float4) <= Math.min(float1, float5);
    }

    public static boolean lineIntersection(Vector2 start1, Vector2 end1, Vector2 start2, Vector2 end2) {
        float float0 = start1.y - end1.y;
        float float1 = end1.x - start1.x;
        float float2 = -float0 * start1.x - float1 * start1.y;
        float float3 = start2.y - end2.y;
        float float4 = end2.x - start2.x;
        float float5 = -float3 * start2.x - float4 * start2.y;
        float float6 = det(float0, float1, float3, float4);
        if (float6 != 0.0F) {
            double double0 = (double)(-det(float2, float1, float5, float4)) * 1.0 / (double)float6;
            double double1 = (double)(-det(float0, float2, float3, float5)) * 1.0 / (double)float6;
            return between(start1.x, end1.x, double0)
                && between(start1.y, end1.y, double1)
                && between(start2.x, end2.x, double0)
                && between(start2.y, end2.y, double1);
        } else {
            return det(float0, float2, float3, float5) == 0.0F
                && det(float1, float2, float4, float5) == 0.0F
                && intersect_1(start1.x, end1.x, start2.x, end2.x)
                && intersect_1(start1.y, end1.y, start2.y, end2.y);
        }
    }

    public static boolean IsQuadranglesAreTransposed2(Vector4f vector4f1, Vector4f vector4f0) {
        if (IsPointInQuadrilateral(new Vector2(vector4f1.x, vector4f1.y), vector4f0.x, vector4f0.z, vector4f0.y, vector4f0.w)) {
            return true;
        } else if (IsPointInQuadrilateral(new Vector2(vector4f1.z, vector4f1.y), vector4f0.x, vector4f0.z, vector4f0.y, vector4f0.w)) {
            return true;
        } else if (IsPointInQuadrilateral(new Vector2(vector4f1.x, vector4f1.w), vector4f0.x, vector4f0.z, vector4f0.y, vector4f0.w)) {
            return true;
        } else if (IsPointInQuadrilateral(new Vector2(vector4f1.z, vector4f1.w), vector4f0.x, vector4f0.z, vector4f0.y, vector4f0.w)) {
            return true;
        } else if (IsPointInQuadrilateral(new Vector2(vector4f0.x, vector4f0.y), vector4f1.x, vector4f1.z, vector4f1.y, vector4f1.w)) {
            return true;
        } else if (IsPointInQuadrilateral(new Vector2(vector4f0.z, vector4f0.y), vector4f1.x, vector4f1.z, vector4f1.y, vector4f1.w)) {
            return true;
        } else {
            return IsPointInQuadrilateral(new Vector2(vector4f0.x, vector4f0.w), vector4f1.x, vector4f1.z, vector4f1.y, vector4f1.w)
                ? true
                : IsPointInQuadrilateral(new Vector2(vector4f0.z, vector4f0.w), vector4f1.x, vector4f1.z, vector4f1.y, vector4f1.w);
        }
    }

    private static boolean IsPointInQuadrilateral(Vector2 vector2, float float0, float float2, float float1, float float3) {
        return IsPointInTriangle(vector2, new Vector2(float0, float1), new Vector2(float0, float3), new Vector2(float2, float3))
            ? true
            : IsPointInTriangle(vector2, new Vector2(float2, float3), new Vector2(float2, float1), new Vector2(float0, float1));
    }

    private static boolean IsPointInTriangle(Vector2 vector20, Vector2 vector21, Vector2 vector22, Vector2 vector23) {
        float float0 = (vector21.x - vector20.x) * (vector22.y - vector21.y) - (vector22.x - vector21.x) * (vector21.y - vector20.y);
        float float1 = (vector22.x - vector20.x) * (vector23.y - vector22.y) - (vector23.x - vector22.x) * (vector22.y - vector20.y);
        float float2 = (vector23.x - vector20.x) * (vector21.y - vector23.y) - (vector21.x - vector23.x) * (vector23.y - vector20.y);
        return float0 >= 0.0F && float1 >= 0.0F && float2 >= 0.0F || float0 <= 0.0F && float1 <= 0.0F && float2 <= 0.0F;
    }
}
